import React from 'react';
import {connect} from 'dva'
import moment from 'moment'
import Tools from 'utils/tools';

import { BasicAbstract } from 'components/Abstract'
import Title from 'components/Title'

import { config } from 'utils';

const { scada } = config;
const { DdToken, Token, IntervalMonth, StationIds } = scada;
// 时间
const formatYMD = 'YYYY-MM-DD';
const time = moment(); // 当前时间
const startOfDay = moment().startOf('month');
const endOfDay = moment().endOf('month');
const startOfMonth = moment().startOf('month').format(formatYMD);
const endOfMonth = moment().endOf('month').format(formatYMD)
const yesterday = moment().add(-1, 'days').format(formatYMD);

@connect(({watermeter}) => ({...watermeter}))
class Watermeter extends React.Component {
    List = [
      {
        label: '小区总数', field: 'village', unit: '个',
        server: { type: 'watermeter/queryRealtimeMeter3', payload: { Token: DdToken, type: 'Unique_Indicators' }}
      },
      { label: '用户总数', field: 'user', unit: '个' },
      { label: '大用户数', field: 'MaxUser', unit: '个' },
      {
        label: '月抄见率', field: 'copyRate', unit: '%', dataResource: 'DivisionCopyrateResourceToday',
        server: { type: 'watermeter/queryDivisionCopyrate', payload: { type: 'Today', Token: DdToken, interval: IntervalMonth, dvs: [] }},
      },
      {
        label: '昨日用水量', field: 'Yesterday', unit: 'm³', dataResource: 'SellWaterResourceYesterday',
        server: { type: 'watermeter/querySellWater', payload: { type: 'Yesterday' } },
      },
      {
        label: '本月用水量', field: 'Month', unit: 'm³', dataResource: 'SellWaterResourceMonth',
        server: { type: 'watermeter/querySellWater', payload: { type: 'Month' } },
      }
    ]

    componentDidMount() {
      this.init();
    }
  
    init = async () => {
      await this.setDateIndicators()
    }
  
    // 获取指标
    setDateIndicators = async () => {
      const { List } = this;
      List.map(ele => {
        const { server } = ele;
        if (server) this.queryServer(server);
      })
    }
  
    queryServer = async (server) => {
      const { type, payload } = server;
      const { type: unType } = payload;

      let payloads = Object.assign({}, payload)
      // 处理 用水量
      if(type === 'watermeter/querySellWater'){
        switch(unType) {
          case 'Yesterday':
             Object.assign(payloads, { startDate: yesterday, endDate: yesterday })
             break;
          case 'Month':
            Object.assign(payloads, { startDate: startOfMonth, endDate: endOfMonth })
             break;
          default:
        } 
      }else if(type === 'watermeter/queryDivisionCopyrate'){
        Object.assign(payloads, { begin: startOfDay.unix(), end: endOfDay.unix() })
      }
      const servers = Object.assign({}, server, { payload: payloads });
      this.props.dispatch(servers);
    }

  // 指标渲染
  render() {
    const { List, props } = this;

    const OverviewInfoResource = props['RealtimeMeter3ResourceUnique_Indicators'];
    let village = 0; // 小区数
    let user = 0;    // 总数
    let MaxUser = 0; // 大表
    if(Array.isArray(OverviewInfoResource)){
      village = OverviewInfoResource[2].v;
      MaxUser = OverviewInfoResource[1].v;
      user = MaxUser + OverviewInfoResource[0].v;
    }
    const dataSource = {
      village: { total: village },
      user: { total: user  },
      MaxUser: {total: MaxUser },
      copyRate: {total: props['DivisionCopyrateResourceToday'] && props['DivisionCopyrateResourceToday'].copyRate ? props['DivisionCopyrateResourceToday'].copyRate:0 },
      Yesterday: { total: Tools.countSellWater(props['SellWaterResourceYesterday']) },
      Month: { total: Tools.countSellWater(props['SellWaterResourceMonth']) },
    }
    return (<Title title={'重点指标监控'}  style={{'paddingLeft': '4%', 'paddingRight': '4%'}}>
      <BasicAbstract
        columns={List}
        dataSource={dataSource}
        isColorful={false}
        className={'col-12'}/>
    </Title>)
  }
}

export default Watermeter;
